package com.luoshizhen.lease.web.app.service.impl;

import com.luoshizhen.lease.model.entity.ViewAppointment;
import com.luoshizhen.lease.web.app.mapper.ViewAppointmentMapper;
import com.luoshizhen.lease.web.app.service.ApartmentInfoService;
import com.luoshizhen.lease.web.app.service.ViewAppointmentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.luoshizhen.lease.web.app.vo.apartment.ApartmentItemVo;
import com.luoshizhen.lease.web.app.vo.appointment.AppointmentDetailVo;
import com.luoshizhen.lease.web.app.vo.appointment.AppointmentItemVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author liubo
 * @description 针对表【view_appointment(预约看房信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ViewAppointmentServiceImpl extends ServiceImpl<ViewAppointmentMapper, ViewAppointment>
        implements ViewAppointmentService {

    @Autowired
    private ViewAppointmentMapper viewAppointmentMapper;

    @Autowired
    private ApartmentInfoService apartmentInfoService;

    /**
     * 根据用户ID列出预约项目
     *
     * 该方法通过用户的ID来查询并返回该用户的所有预约项目信息
     * 它调用了viewAppointmentMapper中的listItemByUserId方法来实现查询逻辑
     *
     * @param userId 用户ID，用于查询预约项目的唯一标识
     * @return 返回一个包含AppointmentItemVo对象的列表，每个对象代表一个预约项目的信息
     */
    @Override
    public List<AppointmentItemVo> listItemByUserId(Long userId) {
        return viewAppointmentMapper.listItemByUserId(userId);
    }

    /**
     * 根据预约ID获取预约详情
     *
     * 此方法首先根据提供的预约ID从数据库中查询预约信息，然后根据预约信息中的公寓ID查询公寓详情，
     * 最后将这两部分信息组合成一个预约详情对象返回
     *
     * @param id 预约的唯一标识符，用于查询特定的预约信息
     * @return 返回一个包含预约详细信息的对象，包括公寓详情
     */
    @Override
    public AppointmentDetailVo getDetailById(Long id) {

        // 根据预约ID查询预约信息
        ViewAppointment viewAppointment = viewAppointmentMapper.selectById(id);

        // 根据预约信息中的公寓ID查询公寓详情
        ApartmentItemVo apartmentItemVo = apartmentInfoService.selectApartmentItemVoById(viewAppointment.getApartmentId());

        // 创建一个预约详情对象
        AppointmentDetailVo agreementDetailVo = new AppointmentDetailVo();
        // 将查询到的预约信息复制到预约详情对象中
        BeanUtils.copyProperties(viewAppointment, agreementDetailVo);

        // 将查询到的公寓详情设置到预约详情对象中
        agreementDetailVo.setApartmentItemVo(apartmentItemVo);

        // 返回包含详细信息的预约详情对象
        return agreementDetailVo;
    }
}




